/**
 * 
 */
package com.geekteam.manga.Utils;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.geekteam.manga.Utils.Constant.SharePreferenceKey;

/**
 * @author MtRol
 *
 */
public class Utils {
	
	public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter(); 
        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }
	
	private static ProgressDialog mDialog;

	public static void createProgress(Context context, String message) {
		mDialog = ProgressDialog.show(context, "", message, true);
		mDialog.show();
	}

	public static void createProgress(Context context, int resId) {
		mDialog = ProgressDialog.show(context, "", context.getString(resId),
				true);
		mDialog.show();
	}

	public static void updateProgress(String message) {
		if (mDialog != null) {
			mDialog.setMessage(message);
		}
	}

	public static void dismissProgress() {
		if (mDialog != null) {
			mDialog.dismiss();
			mDialog = null;
		}
	}
	
	public static SharedPreferences getSharedPreferences(){
		return MangaApplication.getInstance().getSharedPreferences(SharePreferenceKey.MAIN_KEY, Activity.MODE_PRIVATE);
	}
}
